package com.orange.lockscreen;

import java.util.HashMap;
import java.util.Map;

import com.orange.lockscreen.common.Constants.Hanlder_Orientation;
import com.orange.lockscreen.common.Constants.LSWidget_Type;
import com.orange.lockscreen.common.Constants.Slat_Type;
import com.orange.lockscreen.common.LogHelper;

public class SlatHashMap implements Slat_Type, LSWidget_Type,
		Hanlder_Orientation {
	public static final String					TAG						= "SlatHashMap";

	private static int							sScreenHeight			= 0;
	private static int							sScreenWidth			= 0;
	private static int							sOrientation			= LEFT_TO_RIGHT;

	public static final int						TDW_WIDGET_ROWINDEX		= 0;
	public static final int						Event_WIDGET_ROWINDEX	= 1;
	public static final int						MUSIC_WIDGET_ROWINDEX	= 2;
	public static final int						UNLOCK_ROWINDEX			= 3;

	private static Map<Integer, SlatInfo>		sSlatInfos				= new HashMap<Integer, SlatInfo>();
	private static Map<Integer, WidgetViewInfo>	sWidgetViewInfos		= new HashMap<Integer, WidgetViewInfo>();

	public static boolean init(int count, int screenHeight, int screenWidth) {

		if (!sSlatInfos.isEmpty()) {
			sSlatInfos.clear();
		}

		if (screenHeight <= 0) {
			LogHelper.i(TAG, "init. Screen height is less than zero.");
		}
		SlatInfo info = null;
		int height = screenHeight / count;
		for (int i = 0; i < count; i++) {
			info = new SlatInfo();
			info.mHeight = height;
			switch (i) {
			case 0:
				info.mSlatType = TYPE_FIXED;
				break;
			case 1:
			case 2:
				info.mSlatType = TYPE_FIXED_NO_CONTENT;
				break;
			case 3:
				info.mHeight = screenHeight - i * height;
				info.mSlatType = TYPE_UNLOCK;
				break;
			default:
				break;
			}
			sSlatInfos.put(i, info);

			LogHelper.v(TAG, "=== init " + "key=" + i + ", height="
					+ info.mHeight + ", type=" + info.mSlatType);
		}
		sScreenHeight = screenHeight;
		sScreenWidth = screenWidth;

		initWidgetViewInfo();

		return true;
	}

	private static void initWidgetViewInfo() {
		if (!sWidgetViewInfos.isEmpty()) {
			sWidgetViewInfos.clear();
		}
		sWidgetViewInfos.put(TDW_WIDGET_ROWINDEX, new WidgetViewInfo(
				TYPE_TIMEDATE));
		sWidgetViewInfos.put(Event_WIDGET_ROWINDEX, new WidgetViewInfo(
				TYPE_EVENT));
		sWidgetViewInfos.put(MUSIC_WIDGET_ROWINDEX, new WidgetViewInfo(
				TYPE_MUSIC));
	}

	public static boolean restore() {
		int count = sSlatInfos.size();
		init(count, sScreenHeight, sScreenWidth);
		return true;
	}

	public static int getCount() {
		return sSlatInfos.size();
	}

	public static int getScreenHeight() {
		return sScreenHeight;
	}

	public static int getScreenWidth() {
		return sScreenWidth;
	}

	public static int getSlatType(int rowIndex) {
		int type = TYPE_FIXED;
		SlatInfo info = sSlatInfos.get(rowIndex);
		if (null != info) {
			type = info.mSlatType;
		}
		return type;
	}

	/**
	 * Get the actual slat height in screen
	 * 
	 * @param rowIndex
	 * @return
	 */
	public static int getSlatHeight(int rowIndex) {
		int height = 0;
		SlatInfo info = sSlatInfos.get(rowIndex);
		if (null != info) {
			height = info.mHeight;
		}
		return height;
	}

	public static int getSlatTop(int rowIndex) {
		int top = 0;
		SlatInfo info = null;
		for (int i = 0; i < rowIndex; i++) {
			info = sSlatInfos.get(i);
			top += info.mHeight;
		}
		return top;
	}

	public static void setSlatType(int rowIndex, int type) {
		SlatInfo info = sSlatInfos.get(rowIndex);
		if (null != info) {
			info.mSlatType = type;
			sSlatInfos.put(rowIndex, info);
		}
	}

	public static void setSlatHeight(int rowIndex, int height) {
		SlatInfo info = sSlatInfos.get(rowIndex);
		if (null != info) {
			info.mHeight = height;
			sSlatInfos.put(rowIndex, info);
		}
	}

	public static int getHandlerOrientation() {
		return sOrientation;
	}

	public static int getPosByWidgetViewType(int type) {
		int pos = -1;
		boolean isFound = false;
		for (int i = 0; i < sWidgetViewInfos.size(); i++) {
			WidgetViewInfo info = sWidgetViewInfos.get(i);
			if (info.mViewType == type) {
				isFound = true;
			}

			if (isFound) {
				pos = i;
				break;
			}
		}
		return pos;
	}

	static class SlatInfo implements Slat_Type {
		int	mSlatType	= TYPE_FIXED;
		int	mHeight		= 0;
	}

	static class WidgetViewInfo implements Slat_Type, LSWidget_Type {

		int	mViewType	= TYPE_INVALID;

		public WidgetViewInfo(int viewType) {
			mViewType = viewType;
		}
	}
}
